package com.simou.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.simou.common.R;
import com.simou.entity.Employee;
import com.simou.service.Impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployController {
    @Autowired
    EmployeeServiceImpl employeeService;

    @Autowired
    R<Employee> r;

    @RequestMapping("/login")
    @ResponseBody
    private R<Employee> login(HttpSession session, @RequestBody Employee employee){
//        1.对密码进行md5加密
        String password = employee.getPassword();
        String str  = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

//        2.根据姓名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

//        无此员工
        if(emp == null){
            return R.error("无此用户");
        }

        if(!emp.getPassword().equals(str)){
            return R.error("密码错误");
        }

//        用户状态码为0（0代表锁定）
        if(emp.getStatus()==0){
            return R.error("用户被锁定");
        }

//        存储员工id
        session.setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @RequestMapping("/logout")
    public R<String> logOut(HttpSession session){
        session.invalidate();
        return R.success(null);
    }

//    @RequestMapping("/")  与 不加注解代表的路径不一样
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee,HttpSession session){
        // 默认密码为 123456的md5加密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        // 被 MyMetaObjectHandler 的自动填充字段代替
//        employee.setCreateTime(new Timestamp(System.currentTimeMillis()));
//        employee.setUpdateTime(new Timestamp(System.currentTimeMillis()));
//        Object object = session.getAttribute("employee");
//        if(object!=null){
//            Long loginId = (Long) object;
//            employee.setCreateUser(loginId);
//            employee.setUpdateUser(loginId);
//        }

        // 添加employee
        boolean success = employeeService.save(employee);
        if(success){
            return R.success("新增员工成功");
        }

        return R.error(null);
    }

    @GetMapping("/page")
    public R<Page> queryByLimit(int page,int pageSize,String name){
        log.info("page为 {},pageSize为 {},name为 {}",page,pageSize,name);

        // 分页对象
        Page pageInfor = new Page(page, pageSize);

        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryMapper = new LambdaQueryWrapper();
        queryMapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryMapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfor,queryMapper);

        return R.success(pageInfor);
    }

//    修改员工信息
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee,HttpSession session){
        // 被 MyMetaObjectHandler 的自动填充字段代替
//            employee.setUpdateTime(new Timestamp(System.currentTimeMillis()) );
//            Long id = (Long) session.getAttribute("employee");
//            employee.setUpdateUser(id);
            employeeService.updateById(employee);
            return R.success("修改成功");
    }

//    根据id查询员工信息
    @RequestMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable Long id){
        log.info("id:{}",id);

        //        2.根据姓名查询数据库
//        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Employee::getUsername, employee.getUsername());
//        Employee emp = employeeService.getOne(queryWrapper);
        // 根据id查询员工
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Employee::getId,id);
        Employee employee = employeeService.getOne(queryWrapper);
        if(employee==null){
            return R.error("添加信息失败");
        }
        return R.success(employee);
    }


}

